package chapter06;
import java.util.HashSet;
public class Test07_HashSet1 {
    public static void main(String[] args) {
        HashSet<Student> stus = new HashSet<>();
        stus.add(new Student("111","张三"));
        stus.add(new Student("111","张三"));
        stus.add(new Student("112","张三"));
        System.out.println(stus);
    }
}
class Student{
    private String id;
    private String name;
    public Student(String id,String name){
        this.id = id;
        this.name = name;
    }
    @Override
    public int hashCode() {
        return id.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
        if(this==obj){return true;}
        if(obj instanceof Student){
            Student s = (Student)obj;
            if(this.id.equals(s.id)){
                return true;
            }
            return false;
        }
        return false;
    }
    //先重写toString
    @Override
    public String toString() {
        return id+":"+name;
    }
}
